﻿#include "VTabWidget.h"
#include <qtabbar.h>
#include <qtoolbar.h>
#include <qvariant.h>
#include <qdebug.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qicon.h>
#include <QLabel>

VTabWidget::VTabWidget(QWidget*parent)
	: QFrame(parent),
	_toolbar(new QToolBar(this)),
	_tabbar(new QTabBar(this)),
	_title_widget(new QWidget(this)),
	_vlayout(new QVBoxLayout(this)),
	_hlayout(new QHBoxLayout(_title_widget)),
	_hide_action(new QAction(QIcon("d:/close.png"), "隐藏", this)),
	_widget(new QWidget()),
	_widget_layout(new QHBoxLayout(_widget)),
	_expand_widget(new QWidget(this)),
	_expand_widget_layout(new QHBoxLayout(_expand_widget))
{
	init_ui();
	init_connect();
	this->setContentsMargins(0, 0, 0, 0);
	QFile file("d:/1.txt");
	file.open(QFile::ReadOnly);
	QTextStream in(&file);
	in.setCodec("utf8");
	QString text = in.readAll();
	file.close();


	this->setStyleSheet(text);

	// 关联自己隐藏
	connect(_hide_action, &QAction::triggered, [this](){
		this->hide();
		});
	
	// 隐藏自己
	this->hide();
}

VTabWidget::~VTabWidget()
{
	//delete _toolbar;
	//delete _tabbar;
	//delete _hide_button;
}

int VTabWidget::addTab(QString title, QWidget* w, QWidget* expand)
{
	w->setObjectName("test");

	disconnect(_tabbar, &QTabBar::currentChanged, this, &VTabWidget::on_currentChanged);
	int index = _tabbar->addTab(title);
	_expand_map.insert(w, expand);
	_tabbar->setTabData(index, QVariant::fromValue(w));
	_tabbar->setCurrentIndex(index);
	on_currentChanged(index);
	connect(_tabbar, &QTabBar::currentChanged, this, &VTabWidget::on_currentChanged);

	if(this->isVisible() == false){
		this->show();
	}

	return index;
}

QTabBar* VTabWidget::tabBar()
{
	return _tabbar;
}

void VTabWidget::setCurrentIndex(int index)
{
	_tabbar->setCurrentIndex(index);
}

QWidget* VTabWidget::widget(int index)
{
	return qobject_cast<QWidget*>(this->_tabbar->tabData(index).value<QWidget*>());
}



int VTabWidget::count()
{
	return _tabbar->count();
}

QWidget* VTabWidget::currentWidget()
{
	int index = _tabbar->currentIndex();
	if(index == -1)
		return nullptr;
	return qobject_cast<QWidget*>(this->_tabbar->tabData(index).value<QWidget*>());
}

void VTabWidget::removeTab(int index)
{
	if(_tabbar->count() == 0)
		return;

	this->widget(index)->close();
	_tabbar->removeTab(index);

	if(_tabbar->count() == 0){
		this->hide();
		//emit signalCurrentWidgetChanged(nullptr);
	}	
}

void VTabWidget::setTabData(int index, QVariant data)
{
	QWidget* w = this->_tabbar->tabData(index).value<QWidget*>();
	if(w){
		_tabDataMap.insert(w, data);
	}
}

QVariant VTabWidget::tabData(int index)
{
	QWidget* w = this->_tabbar->tabData(index).value<QWidget*>();
	if (w) {
		return _tabDataMap.value(w);
	}
	return QVariant();
}

void VTabWidget::setTabsClosable(bool closable)
{
	_tabbar->setTabsClosable(closable);
}

void VTabWidget::clearLayout(QBoxLayout* layout)
{
	// 清空expand
	QLayoutItem* child;
	while ((child = layout->takeAt(0)) != 0)
	{
		//setParent为NULL，防止删除之后界面不消失
		if (child->widget())
		{
			child->widget()->setParent(NULL);
			child->widget()->hide();
		}
		//layout->removeItem(child);
	}
}

void VTabWidget::setHide(bool hide)
{
	if(hide){
		_toolbar->show();
	}else{
		_toolbar->hide();
	}
}

void VTabWidget::init_ui()
{
	// _title_widget
	_title_widget->setObjectName("vtabwidget_title");

	// 设置tabbar横向为expand
	_tabbar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);

	// 设置标题margin 设置标题的最大高度
	_title_widget->setContentsMargins(0, 0, 0, 0);
	_title_widget->setMaximumHeight(25);

	// 设置hboxlayout
	_hlayout->setMargin(0);
	_hlayout->setSpacing(0);

	// 设置tabbar
	_tabbar->setDrawBase(false);
	_tabbar->setExpanding(false);

	// 设置 expand 边距
	_expand_widget->setContentsMargins(0, 0, 0, 0);
	_expand_widget_layout->setMargin(0);
	_expand_widget_layout->setSpacing(0);

	// 设置title
	_hlayout->addWidget(_tabbar); 
	_hlayout->addWidget(_expand_widget);
	_hlayout->addWidget(_toolbar);
	// 设置center布局
	_vlayout->addWidget(_title_widget);
	_vlayout->addWidget(_widget);
	_vlayout->setMargin(0);
	_vlayout->setSpacing(0);

	// 设置widget边距
	_widget->setObjectName("center");
	_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	_widget->setContentsMargins(0, 0, 0, 0);
	_widget_layout->setMargin(0);
	_widget_layout->setSpacing(0);

	// toolbar添加关闭按钮
	_toolbar->addAction(_hide_action);

	
}

void VTabWidget::init_connect()
{
	//connect(_tabbar, &QTabBar::currentChanged, this, &VTabWidget::on_currentChanged);
}

void VTabWidget::on_currentChanged(int index){
	// 清空主显示区域
	clearLayout(_widget_layout);
	// 清空expand
	clearLayout(_expand_widget_layout);

	QWidget* w = this->_tabbar->tabData(index).value<QWidget*>();
	if (w) {
		this->_widget_layout->addWidget(w);
		w->show();
		w->setFocus();

		// 查询该窗口是否有拓展
		if(_expand_map.value(w)){
			auto temp = _expand_map.value(w);
			_expand_widget_layout->addWidget(_expand_map.value(w));
			_expand_map.value(w)->show();
		}
	}
	
	emit signalCurrentWidgetChanged(w);
}

